/**
 * @file
 * @auth jinguangguo
 * @date 2017/5/11
 */

import Vue from 'vue';

import filter from '../../../../grsc-base/util/filter';
import commonService from '../../../common/commonService';
import service from './service'
import MathTool from '../../../../grsc-base/util/mathtool';
import assetConfig from '../../../common/assetConfig';

export default Vue.extend({
    data() {
        return {
            precision: 0,
            filter: filter,
            coinBalance: '--',
            coinLock:'--',
            btcPrice:'--',
            usdPrice:'--',
            cnyPrice:'--',
            coinType: this.$route.params.coinType.toUpperCase()
        };
    },
    created() {
        this.precision = +this.$$assetMap[this.coinType].WITHDRAWPRECISION;
        this.fetchCoinList();
        this.getTotalAssets();
    },
    watch: {

    },
    methods: {

        /**
         * 获取开放充值个提现的列表
         * */
        fetchCoinList(){
            let that = this;
            let name = this.$router.currentRoute.name;
            let coinList = [];
            if(name === 'deposit'){
                // 充值开启的列表
                $.map(this.$$assetList, (item, index) => {
                    // 0:开启 -1:关闭
                    if (+item[assetConfig.DEPOSITLEVEL_DEFAULT] === 0) {
                        coinList.push(item.assetCode);
                    }
                });
                this.coinList = coinList;
                _doCoinType();
            }else if(name === 'withdraw'){
                service.withdrawAssetList().done(rep=>{
                    if(rep.code === window.CODE.SUCCESS){
                        let results = [];
                        let withdrawList = rep.data;
                        $.map(this.$$assetList, (item, index) => {
                            $.map(withdrawList, (innerAssetCode, innerIndex) => {
                                if (item.assetCode === innerAssetCode) {
                                    results.push(item.assetCode);
                                }
                            });
                        });
                        this.coinList = results;
                        _doCoinType();
                    }
                })
            }

            function _doCoinType() {
                that.coinList.sort();
                let coinType = _.find(that.coinList, function (item) {
                    return item === that.$route.params.coinType.toUpperCase();
                });
                if(coinType){
                    that.coinType = that.$route.params.coinType.toUpperCase();
                }
                // else{
                //     that.coinType = 'BTC';  // 默认使用BTC
                //     that.changeCoinType(that.coinType);
                // }
            }
        },

        /**
         * 获取当前币种价格、可用、冻结
         * */
        getTotalAssets(){
            let that = this;
            $.when(commonService.getBitfinexBtcPrice(), commonService.getTradeDetail(),
                    commonService.getTotalAssets({ assetCode:this.coinType})).done((btcPriceRep, detailRep, assetRep) => {
                if (btcPriceRep.code === window.CODE.SUCCESS) {
                    this.btcPrice = btcPriceRep.data.last_price;
                } else {
                    window.error(btcPriceRep.msg);
                }

                _doCurrentPrice(detailRep);

                if (assetRep.code === window.CODE.SUCCESS) {
                    _doAssetData(assetRep.data);
                } else {
                    window.error(assetRep.msg);
                }
            });

            //获取当前资产对应的btc价格
            function _doCurrentPrice(data){
                let usd = 0;
                if( data['BTC_'+that.coinType] ){
                    let btc = 0;
                    btc = data['BTC_'+that.coinType]['newPrice'];
                    usd =  MathTool.multiplication(btc ,that.btcPrice);
                }else if(data['USDT_'+that.coinType]){
                    usd =  data['USDT_'+that.coinType]['newPrice'];
                }else if(data['USC_'+that.coinType]){
                    usd =  data['USC_'+that.coinType]['newPrice'];
                }
                that.usdPrice = filter.floorFixPrice(usd,2);
                that.cnyPrice = filter.floorFixPrice(MathTool.multiplication(usd,that.CURRENCY_RMB),2);
            }

            function _doAssetData(data) {
                $.map(data, (item, index) => {
                    if (item.assetCode === that.coinType) {
                        that.coinBalance = filter.floorFixPrice(item.amountAvailable, that.precision);
                        that.coinLock = filter.floorFixPrice(item.amountLock, that.precision);

                        if(that.rules&&that.rules.amount){
                            let newTip = that.$t('asset.available')+ ': ' + filter.floorFixPrice(item.amountAvailable, that.precision) + ' ' + that.coinType;
                            that.rules.amount.tip = newTip;
                            that.rules.amount.max = item.amountAvailable;
                            that.rules.amount.maxTip = newTip;
                            that.rules.amount.$setRight(newTip);
                        }
                    }
                });
            }
        },
    }
});